// pages/order/userOrderDetail/orderDetail.js
const App = getApp();
const app = getApp();

let api = require('../../../utils/api.js');

var QQMapWX = require('../../../public/js/qqmap-wx-jssdk.min.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let that = this
   // console.log("options+++++++++++", options)
    if (options.id){

      var orderId = options.id;
      that.setData({
        orderId: orderId
      });
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight
        });
      }
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
var that=this;
    var stateData = [];
    stateData[0] = '待接货';
    stateData[1] = '待装货';
    stateData[2] = '待收货';
    stateData[3] = '待发货';
    stateData[4] = '待送达';
    stateData[5] = '已达到';
    stateData[6] = '待评价';
    var token = wx.getStorageSync('TOKEN');
    var orderId = that.data.orderId;
    // var orderId = 348458871844864;
    var param = {
      id: orderId,
    };
    api._get('/omc/order/detail/' + orderId, param, token).then(res => {

      var detaildata = res.result;
      var shipmentTime = [];
      detaildata['showShipmentTime'] = {};
      shipmentTime['start'] = detaildata.shipmentTime.start * 1000;
      shipmentTime['end'] = detaildata.shipmentTime.end * 1000;
      detaildata['showShipmentTime'] = {
        start: app.formatDate(shipmentTime['start'], 'YYYY-MM-DD H'),
        end: app.formatDate(shipmentTime['end'], 'H')
      }


      var arrivalTime = [];
      detaildata['showArrivalTime'] = [];
      arrivalTime['start'] = detaildata.arrivalTime.start * 1000;
      arrivalTime['end'] = detaildata.arrivalTime.end * 1000;
      detaildata['showArrivalTime'] = {
        start: app.formatDate(arrivalTime['start'], 'YYYY-MM-DD H'),
        end: app.formatDate(arrivalTime['end'], 'H')
      }

      if (detaildata.freightWeight.max && detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.min + '-' + detaildata.freightWeight.max + '吨';
      } else if (detaildata.freightWeight.max && !detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.max + '吨';
      } else if (!detaildata.freightWeight.max && detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.min + '吨';
      }


      if (detaildata.freightBulk.max && detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.min + '-' + detaildata.freightBulk.max + '方';
      } else if (detaildata.freightWeight.max && !detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.max + '方';
      } else if (!detaildata.freightBulk.max && detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.min + '方';
      }



      if (detaildata.remarks){
        detaildata['showRemarks'] = detaildata.remarks;
      }else{
        detaildata['showRemarks'] = '无';
      }
      detaildata['showSpecialAsk'] = '';
      if (detaildata.specialAsk.length != 0) {
        detaildata.specialAsk.forEach((v, i) => {
          if (i == 0) {
            detaildata['showSpecialAsk'] += v;
          } else {
            detaildata['showSpecialAsk'] += ',' + v;
          }
        })
      }
     // console.log(detaildata.state)

      if (detaildata.state < 0) {
        detaildata['showState'] = '取消中';
      } 
      else {
        if (detaildata.state == 6) {
          if (detaildata.isEvaluate == 0) {
            detaildata['showState'] = '待评价';
          } else {
            detaildata['showState'] = '已评价';
          }
        } else {
          detaildata['showState'] = stateData[detaildata['state']];
        }
      }
      var createTime = detaildata['createTime'] * 1000;
      detaildata['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD');

      that.setData({
        detailData: detaildata
      });
     // console.log(res);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
   * 订单操作
   */
  orderAction: function (e) {
   // console.log(e.currentTarget.dataset.orderid);
    var that=this;
    var token = wx.getStorageSync('TOKEN');
    var data = {
      id: e.currentTarget.dataset.orderid,
      state: parseInt(e.currentTarget.dataset.state),
      token: token
    }


    app.orderAction(data).then(function (code) {

     // console.log(code);
      if (code == 200) {
       // console.log(code);


        wx.switchTab({
          url: '/pages/order/order',
          success: function (e) {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) return;
            page.onLoad();
          }
        })
      }
    })

    
  },

  /**
   * 打电话
   */
  callExpert: function (e) {
   // console.log(e.currentTarget.dataset.phone)
    var phone = e.currentTarget.dataset.phone;
    app.callExpert(phone);
  },
  
  showMap: function(e){
   // console.log(e.currentTarget.dataset.address)
    var qqmapsdk = new QQMapWX({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: e.currentTarget.dataset.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function(res) {//成功后的回调
       // console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        wx.getLocation({
          type: 'wgs84', 
          success: function (res) {
            wx.openLocation({//​使用微信内置地图查看位置。
              latitude: latitude,//要去的纬度-地址
              longitude: longitude,//要去的经度-地址
              name: e.currentTarget.dataset.address,
              address: e.currentTarget.dataset.address
            })
          }
        })
      }
    })

   
  },
  // 商议价格操作
  disscussOperating:function(e){
    var that=this;
    var token = wx.getStorageSync('TOKEN');
    var data = {
      orderId: e.currentTarget.dataset.orderid,
      type: parseInt(e.currentTarget.dataset.state),
    }
  
    api._get('/omc/order/operating', data, token).then(res => {
      if(res.code==200){
        that.onShow()
      }else{
        
        wx.showToast({
          title: res.message,
          icon: 'none',
        })
      }
      console.log(res)
    })
  }

})